package com.zhairuihao.common.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.zhairuihao.admin.LoginController;

import javax.servlet.http.HttpSession;

/**
 * .
 *
 * @author zhairuihao
 * @since 2020/6/9
 */

public class LoginInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        HttpSession session = inv.getController().getSession();
        if (session == null) {
            inv.getController().render("/login.html");
        } else {
            String token = (String) session.getAttribute("token");
            if (LoginController.ADMIN_TOKEN.equals(token)) {
                inv.invoke();
            } else {
                inv.getController().render("/login.html");
            }
        }
    }
}
